<?php
class Support extends DataObject{
	static $db = array (
		"Name" => "Varchar(255)",
		"Nick"=>"Varchar(255)",
		"Status"=>"Boolean",
		"Type"=>"Int",

	);
	static $searchable_fields = array (
		'Name'=> array('title'=>'Tên'),
		'Nick'=> array('title'=>'Nick Name'),
	);
	/**
	 * Overload so that the default sort.
	 */
	static $summary_fields = array (
		'Name' => 'Tên',
		'Nick' => 'Nick Name',
		'Type' => 'Loại',
	);
	static $has_one = array (

	);
	static $defaults = array(
		'Type' => '1',);
	function getCMSFields($controller = null) {
		$radio =  new OptionsetField(
		$name ='Type',
		$title = 'Loại',
		$source = array(
               "1" => "Yahoo", 
               "2" => "Skype",
		));
		$ArticleTabVn= new Tab(_t('Support.TABCONTENTTITLEVN', 'Hỗ trợ trực tuyến'),
		$radio,
		new TextField("Name", _t('Support.Name', 'Tên')),
		new TextField("Nick", _t('Support.Nick', 'Nick Name')),
		new CheckboxField("Status",_t('Support.Status', 'Hoạt động'))
		);
		$fields = new FieldSet(new TabSet("Root",$ArticleTabVn));
		return $fields;
	}
	function getCMSValidator() {
		return new RequiredFields(array('Name','Nick'));
	}
	public function yahoo_status($id) {
		$file = @file("http://opi.yahoo.com/online?u=" . urlencode($id) . "&m=t&t=1");
		if (is_array($file)) {
			return (bool) ($file[0] == "01");
		}
		return null;
	}

	public function getSupport()
	{
		$output="";
		$doset=DataObject::get("Support","Status=1 and Type=1");
		if($doset)
		{
			foreach($doset as $row)
			{
				if($row->Nick)
				{
					$status = Support::yahoo_status($row->Nick);
					if($status){
						$output .="<div class=\"support_online\">";
						$output .="<a href=\"ymsgr:sendIM?$row->Nick\">$row->Name</a>";
						$output .="</div>";
					}else{
						$output .="<div class=\"support_offline\">";
						$output .="<a href=\"ymsgr:sendIM?$row->Nick\">$row->Name</a>";
						$output .="</div>";
					}
				}
			}
		}
		return $output;
	}
	public function checkStatusSkype($skypename){
		$temp_file2check = "http://mystatus.skype.com/".$skypename.".num";
		$var = file_get_contents($temp_file2check);
		if($var!= 1 && $var!= 0)
		return true;
		else
		return false;
	}
	public function getSupportSkype()
	{
		$output="";
		$doset=DataObject::get("Support","Status=1 and Type=2");
		if($doset)
		{
			foreach($doset as $row)
			{
				if($row->Nick)
				{
					$status = Support::checkStatusSkype($row->Nick);
					if($status){
						$output .="<div class=\"support_online_skype\">";
						$output .="<a href=\"skype:".$row->Nick."?call\">$row->Name</a>";
						$output .="</div>";
					}else{
						$output .="<div class=\"support_offline_skype\">";
						$output .="<a href=\"skype:".$row->Nick."?call\">$row->Name</a>";
						$output .="</div>";
					}
				}
			}
		}
		return $output;
	}
	function canCreate() {return true;}
	function canEdit() {return true;}
	function canDelete() {return true;}

}
?>